package com.zm.springcloudsentinel.handler;


import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SentinelHandler implements BlockExceptionHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws Exception {
        String msg="未知异常";
        int status=429;
        if (e instanceof FlowException){
            msg="请求限流";
        } else if (e instanceof ParamFlowException) {
            msg="热点参数限流";
        } else if (e instanceof DegradeException) {
            msg="请求降级";
        } else if (e instanceof AuthorityException) {
            msg="无权访问";
            status=401;
        }
        httpServletResponse.setContentType("application/json;charset=utf-8");
        httpServletResponse.setStatus(status);
        httpServletResponse.getWriter().println("{\"msg\":"+msg+",\"status\":"+status+"}");
    }
}
